package cn.project.domain.strategy.service.rule.tree.factory.engine.impl;

import cn.project.domain.strategy.model.valobj.RuleLogicCheckTypeVO;
import cn.project.domain.strategy.model.valobj.RuleTreeNodeLineVO;
import cn.project.domain.strategy.model.valobj.RuleTreeNodeVO;
import cn.project.domain.strategy.model.valobj.RuleTreeVO;
import cn.project.domain.strategy.service.rule.tree.ILogicTreeNode;
import cn.project.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import cn.project.domain.strategy.service.rule.tree.factory.IDecisionTreeEngine;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

/**
 * @author xu
 * @description 决策树引擎
 * @date 2025/7/8
 */
@Slf4j
public class DecisionTreeEngine implements IDecisionTreeEngine {
    // 节点名称 执行节点实体类方法
    private final Map<String, ILogicTreeNode> logicTreeNodeGroup;
    // 规则树信息
    private final RuleTreeVO ruleTreeVO;

    public DecisionTreeEngine(Map<String, ILogicTreeNode> logicTreeNodeGroup, RuleTreeVO ruleTreeVO) {
        this.logicTreeNodeGroup = logicTreeNodeGroup;
        this.ruleTreeVO = ruleTreeVO;
    }

    @Override
    public DefaultTreeFactory.StrategyAwardVO process(String userId, Long strategyId, Integer awardId) {
        DefaultTreeFactory.StrategyAwardVO strategyAwardVO = null;
        // 获取根节点
        String nextNode = ruleTreeVO.getTreeRootRuleNode();
        // 获取起始节点「根节点记录了第一个要执行的规则」
        Map<String, RuleTreeNodeVO> treeNodeVOMap = ruleTreeVO.getTreeNodeMap();
        RuleTreeNodeVO ruleTreeNodeVO = treeNodeVOMap.get(nextNode);
        while (nextNode != null){
            // 获取决策节点
            ILogicTreeNode logicTreeNode = logicTreeNodeGroup.get(ruleTreeNodeVO.getRuleKey());
            String ruleValue = ruleTreeNodeVO.getRuleValue();
            // 决策节点计算
            DefaultTreeFactory.TreeActionEntity treeActionEntity = logicTreeNode.logic(userId, strategyId, awardId,ruleValue);

            RuleLogicCheckTypeVO ruleLogicCheckTypeVO = treeActionEntity.getRuleLogicCheckType();
            strategyAwardVO = treeActionEntity.getStrategyAwardVO();
            log.info("决策树引擎【{}】treeId:{} node:{} code:{}", ruleTreeVO.getTreeName(), ruleTreeVO.getTreeId(), nextNode, ruleLogicCheckTypeVO.getCode());
            // 获取下一个节点
            nextNode = nextNode(ruleLogicCheckTypeVO.getCode(), ruleTreeNodeVO.getTreeNodeLineVOList());
            ruleTreeNodeVO = treeNodeVOMap.get(nextNode);

        }

        // 返回追踪结果
        return strategyAwardVO;
    }

    private String nextNode(String matterValue, List<RuleTreeNodeLineVO> ruleTreeNodeLineVOList){
        if(ruleTreeNodeLineVOList == null || ruleTreeNodeLineVOList.isEmpty()) return null;
        for (RuleTreeNodeLineVO nodeLineVO : ruleTreeNodeLineVOList){
            if(decisionLogic(matterValue, nodeLineVO)){
                return nodeLineVO.getRuleNodeTo();
            }
        }
        // 库存不足时，返回null
        return null;
    }

    public boolean decisionLogic(String matterValue, RuleTreeNodeLineVO nodeLine) {
        switch (nodeLine.getRuleLimitType()) {
            case EQUAL:
                return matterValue.equals(nodeLine.getRuleLimitValue().getCode());
            // 以下规则暂时不需要实现
            case GT:
            case LT:
            case GE:
            case LE:
            default:
                return false;
        }
    }


}
